﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YAMP;

namespace SayisalAnaliz.Library
{
    public static class GrafikYontemi
    {

        public static double Uygula(double baslangic, double degisim, double epsilon, string fonksiyon)
        {
            double enson = baslangic;
            fonksiyon = fonksiyon.Replace(',', '.');
            Parser prs = Parser.Parse(fonksiyon);


            int isaret = Math.Sign(((ScalarValue)prs.Execute(new { x = baslangic })).Value);
            if (isaret == 0)
                return baslangic;
            double hata = 2 * epsilon;
            while (hata > epsilon)
            {
                do
                {

                    hata = ((ScalarValue)prs.Execute(new { x = enson })).Value;
                    enson += degisim;
                    hata = Math.Abs(hata - ((ScalarValue)prs.Execute(new { x = enson })).Value);
                    Console.WriteLine(" hata --> " + hata);
                    if (hata > 100)
                        return -1;

                } while (isaret == Math.Sign(((ScalarValue)prs.Execute(new { x = enson })).Value));
                enson -= degisim;
                degisim = degisim / 2;

            }
            return enson;
        }
    }
}
